<template>
  <div>
    <BasicTable
      @register="registerTable"
      :can-resize="true"
      :bordered="true"
      :striped="false"
      :loading="true"
    >
      <template #toolbar>
        <a-button type="success" size="small" @click="onAddButtonClick()">
          <PlusOutlined /> 新增
        </a-button>
      </template>

      <template #action="{ record }">
        <TableAction
          :actions="[
            {
              icon: 'clarity:note-edit-line',
              label: '编辑',
              onClick: onEditButtonClick.bind(null, record),
            },
            {
              icon: 'ant-design:delete-outlined',
              label: '删除',
              color: 'error',
              popConfirm: {
                title: '是否确认删除',
                confirm: onDeleteButtonClick.bind(null, record),
              },
            },
          ]"
        />
      </template>
    </BasicTable>
    <EditFormModal @register="registerEditFormModal" @success="reload" />
  </div>
</template>
<script lang="ts">
  import { defineComponent } from 'vue';
  import { BasicTable, useTable, TableAction } from '/@/components/Table';

  import { getAllDB, deleteDB } from '/@/api/platform/db';
  import { dbColumns } from './db.data';

  import { QcUnifyTransToChildProps } from '/#/QcUnify';
  import { OperationTypeEnum } from '/@/enums/appEnum';
  import { useMessage } from '/@/hooks/web/useMessage';

  import EditFormModal from './EditModal.vue';

  import { useModal } from '/@/components/Modal';
  import { PlusOutlined } from '@ant-design/icons-vue';

  export default defineComponent({
    name: 'DBManegement',
    components: {
      BasicTable,
      TableAction,
      PlusOutlined,
      EditFormModal,
    },
    setup() {
      const { createMessage } = useMessage();
      const [registerEditFormModal, { openModal: openEditFormModal }] = useModal();

      const [registerTable, { reload }] = useTable({
        title: '数据库管理',
        api: getAllDB,
        columns: dbColumns,
        showTableSetting: true,
        bordered: true,
        size: 'small',
        actionColumn: {
          width: 80,
          title: '操作',
          dataIndex: 'action',
          slots: { customRender: 'action' },
          fixed: 'right', //表格操作列是否固定，不设置时默认固定在右侧
        },
      });

      /**
       * 新增按钮事件，打开新增表单
       */
      function onAddButtonClick() {
        //构造父组件给子组件传值对象
        let propsData: QcUnifyTransToChildProps = {
          moduleName: '数据库管理',
          operateType: OperationTypeEnum.ADD,
        };
        openEditFormModal(true, propsData);
      }

      /**
       * 编辑按钮事件，打开编辑表单
       */
      function onEditButtonClick(record: Recordable) {
        //构造父组件给子组件传值对象
        let propsData: QcUnifyTransToChildProps = {
          moduleName: '数据库管理',
          operateType: OperationTypeEnum.EDIT,
          dataId: record.code,
        };
        openEditFormModal(true, propsData);
      }

      /**
       * 删除按钮事件
       */
      function onDeleteButtonClick(record: Recordable) {
        deleteDB(record.code)
          .then((res) => {
            // console.log('res', res);
            //res判断结果：同时==undefined和null
            if (res == undefined && (res == null || Object.keys(res).length == 0)) {
              createMessage.success('删除成功。', 2);
              reload();
            }
          })
          .catch((ex) => {
            console.log('catch ', ex);
            createMessage.error(ex.msg);
          });
      }

      return {
        registerTable,
        onAddButtonClick,
        onEditButtonClick,
        onDeleteButtonClick,
        registerEditFormModal,
        reload,
      };
    },
  });
</script>
